package net.xdclass.coupon.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.xdclass.common.JsonData;
import net.xdclass.coupon.enums.CouponCategory;
import net.xdclass.coupon.request.GrantCouponRequest;
import net.xdclass.coupon.request.LockCouponRecordRequest;
import net.xdclass.coupon.service.CouponRecordService;
import net.xdclass.coupon.service.CouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yanghuanxi
 * @since 2023-12-05
 */
@Api(tags = "优惠券模块")
@RestController
@RequestMapping("/api/coupon/v1")
public class CouponController {

    @Autowired
    private CouponService couponService;
    @Autowired
    private CouponRecordService couponRecordService;

    @ApiOperation("分页查询优惠券列表")
    @GetMapping("/page")
    public JsonData pageCoupon(@ApiParam("当前页") @RequestParam(value = "page", defaultValue = "1", required = false) int page,
                               @ApiParam("页大小") @RequestParam(value = "size", defaultValue = "10", required = false) int size){
        return couponService.pageCoupon(page, size);
    }

    @ApiOperation("领取优惠券")
    @GetMapping("/promotion/{id}")
    public JsonData promotion(@ApiParam(value = "优惠券id", required = true)
                                  @PathVariable("id") Long id){
        return couponService.drawCoupon(id, CouponCategory.PROMOTION);
    }

    @ApiOperation("RPC-新用户发放优惠券")
    @PostMapping("/user")
    public JsonData grantCouponForNewUser(@ApiParam("用户优惠券请求") @RequestBody GrantCouponRequest request){
        return couponService.grantCoupon(request);
    }

    @ApiOperation("RPC-锁定优惠券库存")
    @PostMapping("/record/lock")
    public JsonData lockCouponRecord(@RequestBody LockCouponRecordRequest request){
        return couponRecordService.lockCouponRecord(request);
    }

}

